package com.cleaningcloud.cld.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.cleaningcloud.cld.domain.CldAssets;
import com.cleaningcloud.cld.domain.CldAssetsRecord;
import com.cleaningcloud.cld.mapper.CldAssetsMapper;
import com.cleaningcloud.cld.mapper.CldAssetsRecordMapper;
import com.cleaningcloud.cld.mapper.PublicCommonMapper;
import com.cleaningcloud.cld.service.ICldAssetsRecordService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldAssetsDetailsMapper;
import com.cleaningcloud.cld.domain.CldAssetsDetails;
import com.cleaningcloud.cld.service.ICldAssetsDetailsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产明细Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-06
 */
@Service
public class CldAssetsDetailsServiceImpl implements ICldAssetsDetailsService 
{
    @Autowired
    private CldAssetsDetailsMapper cldAssetsDetailsMapper;

    @Autowired
    private CldAssetsRecordMapper cldAssetsRecordMapper;


    @Autowired
    private PublicCommonMapper publicCommonMapper;

    @Autowired
    private CldAssetsMapper cldAssetsMapper;

    /**
     * 查询资产明细
     * 
     * @param assetsDetailsId 资产明细主键
     * @return 资产明细
     */
    @Override
    public CldAssetsDetails selectCldAssetsDetailsByAssetsDetailsId(Long assetsDetailsId)
    {
        return cldAssetsDetailsMapper.selectCldAssetsDetailsByAssetsDetailsId(assetsDetailsId);
    }

    /**
     * 查询资产明细列表
     * 
     * @param cldAssetsDetails 资产明细
     * @return 资产明细
     */
    @Override
    public List<CldAssetsDetails> selectCldAssetsDetailsList(CldAssetsDetails cldAssetsDetails)
    {
        return cldAssetsDetailsMapper.selectCldAssetsDetailsList(cldAssetsDetails);
    }

    /**
     * 新增资产明细
     * 
     * @param cldAssetsDetails 资产明细
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldAssetsDetails(CldAssetsDetails cldAssetsDetails)
    {
        // 查询资产信息
        CldAssets cldAssets = cldAssetsMapper.selectCldAssetsByAssetsId(cldAssetsDetails.getAssetsId());
        if(cldAssets == null){
            throw new RuntimeException("资产信息查询失败");
        }
        // 查询资产ID-CARD是否重复
        CldAssetsDetails queryDetails = new CldAssetsDetails();
        queryDetails.setAssetsIdCard(cldAssetsDetails.getAssetsIdCard());
        List<CldAssetsDetails> details = cldAssetsDetailsMapper.selectCldAssetsDetailsList(queryDetails);
        if(!details.isEmpty()){
            throw new RuntimeException("资产编号数据库已存在,请重新输入");
        }
        cldAssetsDetails.setBulMoney(cldAssets.getBulMoney());
        cldAssetsDetails.setCreateTime(DateUtils.getNowDate());
        cldAssetsDetails.setUpdateTime(cldAssetsDetails.getCreateTime());
        cldAssetsDetails.setCreateId(SecurityUtils.getUserId());
        cldAssetsDetails.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        cldAssetsDetails.setCreateName(SecurityUtils.getUsername());
        cldAssetsDetails.setIsDepreciation(2L);// 1=是 2=否
        cldAssetsDetails.setFlag(0L);
        cldAssetsDetails.setAssetsIdCard(cldAssetsDetails.getAssetsNo()+"-"+cldAssetsDetails.getAssetsIdCard()+"-"+ new SimpleDateFormat("ssSSS").format(new Date()));
        CldAssetsRecord cldAssetsRecord = getCldAssetsRecord(cldAssetsDetails);
        cldAssetsRecordMapper.insertCldAssetsRecord(cldAssetsRecord);
        return cldAssetsDetailsMapper.insertCldAssetsDetails(cldAssetsDetails);
    }

    private static CldAssetsRecord getCldAssetsRecord(CldAssetsDetails cldAssetsDetails) {
        CldAssetsRecord cldAssetsRecord=new CldAssetsRecord();
        cldAssetsRecord.setAssetsId(cldAssetsDetails.getAssetsId());
        cldAssetsRecord.setAssetsIdCard(cldAssetsDetails.getAssetsIdCard());
        cldAssetsRecord.setFlag(0L);
        //1闲置中2投放中3维修中4已报废
        cldAssetsRecord.setRecordStatus(1L);
        cldAssetsRecord.setLdleStartTime(new Date());
        cldAssetsRecord.setOperaUserId(cldAssetsDetails.getCreateId());
        cldAssetsRecord.setOperaUserTime(new Date());
        cldAssetsRecord.setIsNewUpdate(1L);
        return cldAssetsRecord;
    }

    /**
     * 修改资产明细
     * 
     * @param cldAssetsDetails 资产明细
     * @return 结果
     */
    @Override
    public int updateCldAssetsDetails(CldAssetsDetails cldAssetsDetails)
    {
        cldAssetsDetails.setAssetsIdCard(cldAssetsDetails.getAssetsNo()+"-"+cldAssetsDetails.getAssetsIdCard());
        return cldAssetsDetailsMapper.updateCldAssetsDetails(cldAssetsDetails);
    }

    /**
     * 批量删除资产明细
     * 
     * @param assetsDetailsIds 需要删除的资产明细主键
     * @return 结果
     */
    @Override
    public int deleteCldAssetsDetailsByAssetsDetailsIds(Long[] assetsDetailsIds)
    {
        return cldAssetsDetailsMapper.deleteCldAssetsDetailsByAssetsDetailsIds(assetsDetailsIds);
    }

    /**
     * 删除资产明细信息
     * 
     * @param assetsDetailsId 资产明细主键
     * @return 结果
     */
    @Override
    public int deleteCldAssetsDetailsByAssetsDetailsId(Long assetsDetailsId)
    {
        return cldAssetsDetailsMapper.deleteCldAssetsDetailsByAssetsDetailsId(assetsDetailsId);
    }
}
